package com.kotlin.network.retrofit;

import com.kotlin.network.NetConstants;
import com.kotlin.network.retrofit.api.ApiService;
import java.util.concurrent.TimeUnit;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;
/**
 * Date: 16/7/2 18:17
 */
public class RetroFactory {

	private static final String TAG = RetroFactory.class.getSimpleName();
	private static final int DEFAULT_TIMEOUT = 20;

	private static ApiService apiServiceInstance;//常规业务逻辑调用
	private static ApiService apiNoJsonServiceInstance;

	//private static ApiServiceV2 apiServiceV2Instance;//常规业务逻辑调用

	public static ApiService getApiServiceInstance() {
		if (apiServiceInstance == null) {
			apiServiceInstance = getRetrofit(ApiType.NORMAL,"v1",true).create(ApiService.class);
		}
		return apiServiceInstance;
	}

	public static ApiService getApiNoJsonServiceInstance() {
		if (apiNoJsonServiceInstance == null) {
			apiNoJsonServiceInstance = getRetrofit(ApiType.NORMAL, "v1", false).create(ApiService.class);
		}
		return apiNoJsonServiceInstance;
	}


	//public static ApiServiceV2 getApiServiceInstanceV2() {
	//	if (apiServiceV2Instance == null) {
	//		apiServiceV2Instance = getRetrofit(ApiType.NORMAL,"v2").create(ApiServiceV2.class);
	//	}
	//	return apiServiceV2Instance;
	//}




	/**
	 * 获取retrofit实例
	 *
	 * @param type 请求的类型
	 * @return
	 */
	private static Retrofit getRetrofit(ApiType type,String version, boolean parseJson) {

		//******HTTPS*****************************
		//InputStream[] is = new InputStream[0];
		//try {
		//	is = new InputStream[] {
		//			MApplication.gainContext().getAssets().open("ecovacs.cer")
		//	};
		//} catch (IOException e) {
		//	e.printStackTrace();
		//}
		//HttpsUtils.SSLParams sslParams = HttpsUtils.getSslSocketFactory(is, null, null);
		//***********************************
		//手动创建一个OkHttpClient并设置超时时间
		OkHttpClient.Builder builder = new OkHttpClient.Builder();
		builder.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);

		//if (BuildConfig.DEBUG) {
		//	builder.addNetworkInterceptor(new StethoInterceptor());
		//}


		//公共参数
		builder.addInterceptor(getInterceptor(type));
		//******HTTPS*****************************
		//		.hostnameVerifier((hostname, session) -> true)
		//		.sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager);
		//******HTTPS*****************************

		Retrofit.Builder retrofitBuilder =  new Retrofit.Builder()
				.client(builder.build())
				//.client(getUnsafeOkHttpClient())
				.addConverterFactory(GsonConverterFactory.create())
				.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
				.baseUrl(getBaseUrl(type,version));
				//.build();

		if (parseJson) {
			retrofitBuilder.addConverterFactory(GsonConverterFactory.create());
		} else {
			retrofitBuilder.addConverterFactory(ScalarsConverterFactory.create());
		}
		return retrofitBuilder.build();
	}

	/**
	 * 获取 baseUrl
	 *
	 * @param type 请求的类型
	 * @return
	 */
	private static String getBaseUrl(ApiType type,String version) {
		switch (type) {
			case NORMAL:
			return NetConstants.appPrivateInterface ;//
			default:
				return NetConstants.appPrivateInterface;
		}
	}

	/**
	 * 获取拦截器
	 *
	 * @param type 请求的类型
	 * @return
	 */
	private static Interceptor getInterceptor(ApiType type) {
		return new SigningInterceptor(type);
	}

	/**
	 * 请求的类型
	 * 不同的类型对应不同的url等
	 */
	public enum ApiType {
		NORMAL, //常用的
		UPDATE, //更新相关
		AUTH, //认证相关
		PUSH, //推送
		UPLOAD, //数据上报
		TEST  //调试用
	}


//-------------------------------------------------------------



}